clear all
global system "linux"

if "${system}" == "linux" {
	global code "/"
	global s "/"
}
clear

set obs 1
gen ltvprev_diff = 1
gen Cutoff = 1
gen notba = 1

run _esttab-latex-options.do

foreach Stage of numlist 1/3 {	
	foreach Sample of numlist 6/6 {
		
		eststo clear
	
		local mgroup ""
		local pattern ""
		local mtitle ""
		
		foreach WindowType of numlist 1/3 {
			foreach Poly of numlist 1/3 {
				estimate use "${Estimates}/harp-ltvprev_diff-Stage`Stage'-Window`WindowType'-Poly`Poly'-Sample`Sample'"
				quietly {
				eststo
					
				estadd local llpa "Y"
				estadd local zip_3 "Y"
				estadd local lender "Y"
				estadd local month "Y"
				estadd local control "Y"
				}
				
				if `Poly' == 1 local patternadd = 1
				if `Poly' > 1 local patternadd = 0
				local pattern "`pattern' `patternadd'"
				local mtitle "`mtitle' `"Poly `Poly'"'"
			}
			local mgroup "`mgroup' `"$ MaxLTV \in [`=95+(`WindowType'-1)*2.5', `=115-(`WindowType'-1)*2.5']$"'"
		}
		if inlist(`Stage', 1, 2) local keepvar "1.Cutoff#c.ltvprev_diff"
		if inlist(`Stage', 3) local keepvar "notba"
		
		qui do "${code}/_labels"
		label var Cutoff "$ z_i$"
		
		esttab using "${table}/harp-Stage`Stage'-Sample`Sample'.tex", replace keep(`keepvar') coeflabels(1.Cutoff#c.ltvprev_diff "$ z_i \times 1[z_i>0]$")  ///
		 ${opts_esttab}  b(%9.3fc) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
		 stats(/* llpa */ zip_3 lender month control N r2_a, fmt(/* %1s */ %1s %1s %1s %1s %9.0fc %9.3fc) ///
		 labels(/* "LLPA FE" */ "ZIP3 FE" "Lender FE" "Month FE" "Other Controls" "\midrule N. Obs." "Adj. $ R^2$")) ///
		 mgroups(`mgroup', pattern(`pattern') ${opts_mgroups}) ///
		 mtitle(`mtitle')
	}
}

